package com.hut.oa.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hut.oa.common.StringUtil;
import com.hut.oa.model.bean.Directory;
import com.hut.oa.model.bean.FileManage;
import com.hut.oa.model.bean.User;
import com.hut.oa.model.dao.file.DirectoryDao;
import com.hut.oa.model.dao.file.FileManageDao;
import com.hut.oa.model.dao.user.UserDao;
import com.hut.oa.model.mapper.FilemanageMapper;
import com.hut.oa.service.AnnounceService;
import com.hut.oa.service.DirectoryService;
import com.hut.oa.service.FileService;

@Controller
@RequestMapping("/file")
public class FileController {
	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private DirectoryDao dirdao;

	@Autowired
	private FileService fileservice;

	@Autowired
	private DirectoryService dirservice;

	@Autowired
	private FileManageDao filedao;

	@Autowired
	private FilemanageMapper filemapper;

	@Autowired
	private UserDao udao;
	@Value(value = "${file.upload.path}")
	private String rootpath;

	@RequestMapping("/file")
	public String toFile(Model model, HttpServletResponse response, HttpSession session) throws IOException {
		
		Directory dir = new Directory();
		User u = (User) session.getAttribute("userinfo");
		Directory origndir = dirdao.findByUserAndPidIsNull(u);
		if (origndir == null) {
			Directory newfolder = new Directory();
			newfolder.setText("全部文件");
			newfolder.setPath("");
			newfolder.setUser(u);
			dirdao.save(newfolder);
		}
		List<Directory> directlist = dirdao.selectAlldirect();
		List<Directory> fatherdir = dirdao.findByPidIsNullAndUserAndIsDelete(u,0);
		dir.setId(fatherdir.get(0).getId());
		List<FileManage> filelistzero = filedao.findByDirAndIsDeleteAndUser(dir,0,u);

		model.addAttribute("dirid", fatherdir.get(0).getId());

		List<FileManage> allfilelist = filedao.selectAllfile();
		model.addAttribute("directlist", directlist);
		model.addAttribute("filelist", filelistzero);
		model.addAttribute("allfilelist", allfilelist);
		return "file/file";
	}

	@RequestMapping("/image/**")
	public void image(Model model, HttpServletResponse response, HttpSession session, HttpServletRequest request)
			throws IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String startpath = new String(URLDecoder.decode(request.getRequestURI(), "utf-8"));
		String path = startpath.replace("/file/image", "");
		System.out.println(path);
		File f = new File(rootpath, path);
		ServletOutputStream sos = response.getOutputStream();
		FileInputStream input = new FileInputStream(f.getPath());
		byte[] data = new byte[(int) f.length()];
		IOUtils.readFully(input, data);
		// 将文件流输出到浏览器
		IOUtils.write(data, sos);
		input.close();
		sos.close();
	}

	/**
	 * 到添加文件页面
	 * 
	 * @return
	 */
	@RequestMapping("/addfile")
	public String toAddFile() {
		return "file/upload";
	}

	// 上传文件
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public void upload(@RequestParam("image") MultipartFile f, @RequestParam("dirId") String dirid,
			@RequestParam("path") String path, Model model, HttpSession session)
			throws IllegalStateException, IOException {
		/*
		 * logger.info("s",""); logger.info("",f.getName()); String
		 * suffix=FilenameUtils.getExtension(f.getOriginalFilename());
		 * logger.info(suffix,"");
		 */
		Directory dir = new Directory();
		User u = (User) session.getAttribute("userinfo");
		dir.setId(Long.parseLong(dirid));
		if (path.equals("undefined")) {
			fileservice.save(f, "upload\\"+u.getId()+"", dir, u);
		}else {
			fileservice.save(f, path, dir, u);
		}
	}

	// 新建文件夹
	@RequestMapping(value = "/newfolder", method = RequestMethod.POST)
	public void newfolder(@RequestParam("dirId") String fatherid, @RequestParam("path") String path,
			@Param("name") String name, Model model, HttpSession session) throws IllegalStateException, IOException {
		/*
		 * logger.info("s",""); logger.info("",f.getName()); String
		 * suffix=FilenameUtils.getExtension(f.getOriginalFilename());
		 * logger.info(suffix,"");
		 */
		User u = (User) session.getAttribute("userinfo");
		fileservice.newfolder(path, fatherid, name, u);
	}

	// 新建树
	@RequestMapping(value = "/tree")
	public @ResponseBody List<Directory> newtree(HttpSession session) {
		User u = (User) session.getAttribute("userinfo");
		List<Directory> list = fileservice.tree(u);
		logger.info(list + "");
		return list;
	}

	// 下载
	@RequestMapping(value = "/download/**", method = RequestMethod.GET)
	public void download(@RequestParam("path") String path, @RequestParam("directid") String fileid,
			@RequestParam("filesuffix") String filesuffix, Model model, HttpServletResponse response)
			throws UnsupportedEncodingException {
		if (filesuffix.equals("undefined")) {
			File f = new File(this.rootpath, URLDecoder.decode(path, "utf-8"));
		} else {
			File f = new File(this.rootpath, URLDecoder.decode(path, "utf-8"));
			FileManage file = filedao.findOne(Long.parseLong(fileid));
			try {
				// 设置下载的请求头信息
				// 显示文件大小
				response.setContentLength(file.getSize().intValue());
				// 下载时显示文件类型
				response.setContentType(file.getFiletype());
				response.setHeader("Content-Disposition",
						"attachment;filename=" + new String(file.getFilename().getBytes("UTF-8"), "ISO8859-1"));
				ServletOutputStream sos = response.getOutputStream();
				// 读取文件问字节码
				byte[] data = new byte[file.getSize().intValue()];
				FileInputStream input = new FileInputStream(f);
				IOUtils.readFully(input, data);
				// 将文件流输出到浏览器
				IOUtils.write(data, sos);
				input.close();
				sos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
	// 查找子文件夹
	@RequestMapping(value = "/son", method = RequestMethod.POST)
	public @ResponseBody List<Directory> selectson(@RequestParam("directid") String id, Model model,
			HttpSession session) {
		List<Directory> list = new ArrayList<>();
		User u = (User) session.getAttribute("userinfo");
		Directory dir = new Directory();
		dir.setId(Long.parseLong(id));
		list = dirdao.findByPidAndIsDelete(dir,0);
		return list;
	}

	// 重命名
	@RequestMapping(value = "/newname", method = RequestMethod.POST)
	@ResponseBody
	public void newname(@RequestParam("dirid") String id, @RequestParam("value") String value,
			@RequestParam(value="filesuffix",required=false) String filesuffix) {
		if (null!=filesuffix) {
			fileservice.newname(value, Long.parseLong(id));
		} else{
			dirservice.updatename(id, value);
		}
	}
	// 加载文件夹树
	@RequestMapping(value = "/loadtree", method = RequestMethod.POST)
	public @ResponseBody List<Directory> loadtree() {
		List<Directory> alldir = dirdao.selectAlldirect();
		return alldir;
	}

	// 查找文件
	@RequestMapping(value = "/sonfile", method = RequestMethod.POST)
	public @ResponseBody List<FileManage> selectsonfile(HttpSession session,@RequestParam("directid") String id, Model model) {
		Directory dir = new Directory();
		dir.setId(Long.parseLong(id));
		User u=(User)session.getAttribute("userinfo");
		List<FileManage> list = filedao.findByDirAndIsDelete(dir,0);
		return list;
	}

	// 删除
	@RequestMapping(value = "/filedelete", method = RequestMethod.POST)
	@ResponseBody	
	public void deletefile(HttpSession session,@RequestParam("directid") String id, @RequestParam("suffix") String suffix, Model model) {
		User u=(User)session.getAttribute("userinfo");
		if (suffix.equals("undefined")) {
			Directory dir = dirdao.findById(Long.parseLong(id));
			List<Directory> filedir=dirdao.findByPidIsNullAndUserAndIsDelete(u,0);
			dir.setIsDelete(1);
			dir.setPid(filedir.get(0));
			dirdao.save(dir);
			List<FileManage> fatherfile = filedao.findByDir(dir);
			for (FileManage fileManage : fatherfile) {
				fileManage.setIsDelete(1);
				filedao.save(fileManage);
			}
			List<Directory> fatherdir = dirdao.findByPid(dir);
			this.delete(fatherdir,1);
		} else {
			List<Directory> dir=dirdao.findByPidIsNullAndUserAndIsDelete(u,0);
			fileservice.updateid(Long.parseLong(id),dir.get(0),1);
		}
	}
	//彻底删除
	@RequestMapping(value = "/realydelete", method = RequestMethod.POST)
	@ResponseBody
	public void realydelete(HttpSession session,@RequestParam(value="file",required=false) String[] file, @RequestParam(value="folder",required=false) String[] folder,@RequestParam("type") String type) {
		User u=(User)session.getAttribute("userinfo");
		if(type.equals("删除")){
			if(null!=file){
				for (int i = 0; i < file.length; i++) {
					File f = new File(this.rootpath, filedao.findOne(Long.parseLong(file[i])).getPath());
					fileservice.deletefolder(f);
					filedao.delete(Long.parseLong(file[i]));
				}
			}	
			if(null!=folder){
				for (int i = 0; i < folder.length; i++) {	
					File f = new File(this.rootpath, dirdao.findOne(Long.parseLong(folder[i])).getPath());
					fileservice.deletefolder(f);
					Directory dir = dirdao.findById(Long.parseLong(folder[i]));
					List<FileManage> fatherfile = filedao.findByDir(dir);
					for (FileManage fileManage : fatherfile) {
						filedao.delete(fileManage);
					}
					List<Directory> fatherdir = dirdao.findByPid(dir);			
					this.deletefunction(fatherdir);
					dirdao.delete(dir);				
				}
			}
		}else{
			if(null!=file){
				for (int i = 0; i < file.length; i++) {
					List<Directory> dir=dirdao.findByPidIsNullAndUserAndIsDelete(u,0);
					filemapper.deletebyfileid(Long.parseLong(file[i]));
					fileservice.updateid(Long.parseLong(file[i]),dir.get(0),0);
				}
			}
			if(null!=folder){
				for (int i = 0; i < folder.length; i++) {		
					Directory dir = dirdao.findById(Long.parseLong(folder[i]));
					filemapper.deletebydirid(Long.parseLong(folder[i]));
					List<Directory> filedir=dirdao.findByPidIsNullAndUserAndIsDelete(u,0);
					dir.setIsDelete(0);
					dir.setPid(filedir.get(0));
					dirdao.save(dir);			
					List<FileManage> fatherfile = filedao.findByDir(dir);
					for (FileManage fileManage : fatherfile) {
						filemapper.deletebyfileid(fileManage.getId());
						fileManage.setIsDelete(0);
						filedao.save(fileManage);
					}
					List<Directory> fatherdir = dirdao.findByPid(dir);
					this.delete(fatherdir,0);			
				}		
			}
		}
		
	}
	// 删除
	@RequestMapping(value = "/deleteicon", method = RequestMethod.POST)
	@ResponseBody
	public void deletefile(HttpSession session,@RequestParam("file") String[] file, @RequestParam("folder") String[] folder, Model model) {
		User u=(User)session.getAttribute("userinfo");
		if (!file[0].equals("undefiend")) {
			for (int i = 0; i < file.length; i++) {
				List<Directory> dir=dirdao.findByPidIsNullAndUserAndIsDelete(u,0);
				filemapper.deletebyfileid(Long.parseLong(file[i]));
				fileservice.updateid(Long.parseLong(file[i]),dir.get(0),1);
			}
		}
		if (!folder[0].equals("undefiend")) {
			for (int i = 0; i < folder.length; i++) {		
				Directory dir = dirdao.findById(Long.parseLong(folder[i]));
				filemapper.deletebydirid(Long.parseLong(folder[i]));
				List<Directory> filedir=dirdao.findByPidIsNullAndUserAndIsDelete(u,0);
				dir.setIsDelete(1);
				dir.setPid(filedir.get(0));
				dirdao.save(dir);			
				List<FileManage> fatherfile = filedao.findByDir(dir);
				for (FileManage fileManage : fatherfile) {
					filemapper.deletebyfileid(fileManage.getId());
					fileManage.setIsDelete(1);
					filedao.save(fileManage);
				}
				List<Directory> fatherdir = dirdao.findByPid(dir);
				this.delete(fatherdir,1);			
			}
		}
	}
	// 移动
	@RequestMapping(value = "/movefile", method = RequestMethod.POST)
	@ResponseBody
	public void movefile(@RequestParam("fileid") String fileid, @RequestParam("filename") String filename,
			@RequestParam("filepath") String filepath, @RequestParam("suffix") String suffix,
			@RequestParam("id") String id, @RequestParam("directpath") String directpath, Model model)
			throws IOException {
		if (suffix.equals("undefined")) {
			Directory fatherdir = dirdao.findById(Long.parseLong(fileid));
			Map<String, String> map = fileservice.copyfolder(fatherdir, directpath, id);
			List<FileManage> sonfile = filedao.findByDir(fatherdir);
			for (FileManage f : sonfile) {
				fileservice.copy(f.getId(), map.get("id"), map.get("path"));
				filedao.delete(f);
			}
			List<Directory> sondir = dirdao.findByPid(fatherdir);
			this.cursion(sondir, map);
			this.move(sondir);
			dirdao.delete(fatherdir);
			File file = new File(this.rootpath, fatherdir.getPath());
			fileservice.deletefolder(file);
		} else {
			fileservice.deletefile(Long.parseLong(fileid), id, directpath);
			filedao.delete(Long.parseLong(fileid));
		}

	}
	// 复制
	@RequestMapping(value = "/copyfile", method = RequestMethod.POST)
	@ResponseBody
	public void copyfile(@RequestParam("fileid") String fileid, @RequestParam("filename") String filename,
			@RequestParam("filepath") String filepath, @RequestParam("suffix") String suffix,
			@RequestParam("id") String id, @RequestParam("directpath") String directpath, Model model)
			throws IOException {
		if (suffix.equals("undefined")) {
			Directory fatherdir = dirdao.findById(Long.parseLong(fileid));

			Map<String, String> map = fileservice.copyfolder(fatherdir, directpath, id);
			List<FileManage> sonfile = filedao.findByDir(fatherdir);
			for (FileManage f : sonfile) {
				fileservice.copy(f.getId(), map.get("id"), map.get("path"));
			}
			List<Directory> sondir = dirdao.findByPid(fatherdir);
			this.cursion(sondir, map);
		} else {
			fileservice.copy(Long.parseLong(fileid), id, directpath);
		}

	}

	// 文件类型选择
	@RequestMapping(value = "/filetype", method = RequestMethod.POST)
	public @ResponseBody List<FileManage> filetype(@RequestParam("filetype") String filetype, HttpSession session) {
		User u = (User) session.getAttribute("userinfo");

		if (filetype.equals("text")) {
			return filedao.selecttext(u);

		} else if (filetype.equals("image")) {
			return filedao.selectimage(u);

		} else if (filetype.equals("music")) {
			return filedao.selectmp4(u);

		} else if (filetype.equals("mp3")) {
			return filedao.selectmp3(u);
		}else {
			return filedao.selectzip(u);
		}
	}

	// 分享公司文件
	@RequestMapping(value = "/share", method = RequestMethod.POST)
	@ResponseBody
	public void sharefile(@RequestParam("filesuffix") String filesuffix, @RequestParam("directid") String directid,
			HttpSession session) {
		User u = (User) session.getAttribute("userinfo");
		if (filesuffix.equals("undefined")) {
			Directory dir = dirdao.findById(Long.parseLong(directid));
			dir.setDirpermission("1");
			dirdao.save(dir);
		} else {
			FileManage file = filedao.findById(Long.parseLong(directid));
			file.setPermission(1 + "");
			filedao.save(file);
		}

	}

	// 公司文件
	@RequestMapping(value = "/companyfile", method = RequestMethod.POST)
	public @ResponseBody List<FileManage> companyfile() {
		return filedao.findByPermissionAndIsDelete("1",0);
	}
	// 公司文件夹
	@RequestMapping(value = "/companyfolder", method = RequestMethod.POST)
	public @ResponseBody List<Directory> companyfolder() {
		return dirdao.findByDirpermissionAndIsDelete("1",0);
	}

	// 生成联系人
	@RequestMapping(value = "/user", method = RequestMethod.POST)
	public @ResponseBody List<User> shareuser(HttpSession session) {
		User u = (User) session.getAttribute("userinfo");
		List<User> ulist = udao.findByIdNot(u.getId());
		return ulist;
	}

	// 共享文件
	@RequestMapping(value = "/shareuser", method = RequestMethod.POST)
	@ResponseBody
	public void shareuserfile(HttpSession session, @RequestParam("ulist") String[] ulist,
			@RequestParam("suffix") String suffix, @RequestParam("directid") String directid) {
		List<User> userlist = new ArrayList<>();
		for (int i = 0; i < ulist.length; i++) {
			userlist.add(udao.findOne(Long.parseLong(ulist[0])));
		}
		for (int i = 0; i < ulist.length; i++) {
			if (suffix.equals("undefined")) {
				Directory dir = dirdao.findById(Long.parseLong(directid));
				dir.setDiruser(userlist);
				dirdao.save(dir);
				
			} else {
				FileManage file = filedao.findById(Long.parseLong(directid));
				file.setFileuser(userlist);
				filedao.save(file);
			}
		}
	}

	// 生成共享文件
	@RequestMapping(value = "/shareuserfile", method = RequestMethod.POST)
	public @ResponseBody List<FileManage> usersharefile(HttpSession session) {
		User u = (User) session.getAttribute("userinfo");
		List<FileManage> flist = new ArrayList<>();
		List<Map<String, Object>> list = filemapper.selectfilebyid(u.getId());
		for (Map<String, Object> map : list) {
				flist.add(filedao.findById(Long.parseLong(map.get("fu_file_id").toString())));
			}
		return flist;
	}

	// 生成共享文件夹
	@RequestMapping(value = "/shareuserfolder", method = RequestMethod.POST)
	@ResponseBody
	public  List<Directory> shareuserfolder(HttpSession session) {
		User u = (User) session.getAttribute("userinfo");
		List<Directory> flist = new ArrayList<>();
		List<Map<String, Object>> list = filemapper.selectfolderbyid(u.getId());
		for (Map<String, Object> map : list) {
				flist.add(dirdao.findById(Long.parseLong(map.get("du_directory_id").toString())));
		}
		return flist;
	}

	// 查找文件
	@RequestMapping(value = "/searchfile", method = RequestMethod.POST)
	public @ResponseBody List<FileManage> searchfile(@RequestParam("value") String value, HttpSession session) {
		User u = (User) session.getAttribute("userinfo");
		List<FileManage> dirlist = filedao.findlikefilenameanduser(value, u);
		return dirlist;
	}

	// 查找文件夹
	@RequestMapping(value = "/searchfolder", method = RequestMethod.POST)
	@ResponseBody
	public  List<Directory> searchfolder(@RequestParam("value") String value, HttpSession session) {
		User u = (User) session.getAttribute("userinfo");
		List<Directory> dirlist = dirdao.findlikeTextanduser(value, u);
		return dirlist;
	}

	// 查找共享文件
	@RequestMapping(value = "/searchsharefile", method = RequestMethod.POST)
	public @ResponseBody List<FileManage> searchsharefile(@RequestParam("value") String value, HttpSession session) {
		User u = (User) session.getAttribute("userinfo");
		List<FileManage> flist = new ArrayList<>();
		List<Map<String, Object>> list = filemapper.selectfilebyid(u.getId());
		for (Map<String, Object> map : list) {
			flist.add(filedao.findByIdAndFilename(Long.parseLong(map.get("fu_file_id") + ""), value));
		}
		return flist;
	}

	// 查找共享文件夹
	@RequestMapping(value = "/searchsharefolder", method = RequestMethod.POST)
	public @ResponseBody List<Directory> searchsharefolder(@RequestParam("value") String value, HttpSession session) {
		User u = (User) session.getAttribute("userinfo");
		List<Directory> flist = new ArrayList<>();
		List<Map<String, Object>> list = filemapper.selectfolderbyid(u.getId());
		for (Map<String, Object> map : list) {
			flist.add(dirdao.findByIdAndText(Long.parseLong(map.get("du_directory_id") + ""), value));
		}
		return flist;
	}

	// 查找公司文件
	@RequestMapping(value = "/searchcompanyfile", method = RequestMethod.POST)
	public @ResponseBody List<FileManage> searchcompanyfile(@RequestParam("value") String value, HttpSession session) {
		List<FileManage> file = filedao.findlikeFilename("1", value);
		return file;
	}

	// 查找公司文件夹
	@RequestMapping(value = "/searchcompanyfolder", method = RequestMethod.POST)
	public @ResponseBody List<Directory> searchcompanyfolder(@RequestParam("value") String value, HttpSession session) {
		List<Directory> directory = dirdao.findlikeText("1", value);
		return directory;
	}
	//查找指定人
	@RequestMapping(value = "/searchshareuser", method = RequestMethod.POST)
	public @ResponseBody List<User> searchshareuser(@RequestParam("inputval")String inputval,HttpSession session) {	
		List<User> ulist=udao.findByRealNameOrTel(inputval);
		return ulist;
	}
	//垃圾箱文件夹生成
	
	@RequestMapping(value="/gabgefolder",method=RequestMethod.POST)
	public @ResponseBody List<Directory> gabagefolder(HttpSession session) {
		User u=(User)session.getAttribute("userinfo");
		Directory dir=dirdao.findByUserAndPidIsNull(u);
		return dirdao.findByIsDeleteAndUserAndPid(1,u,dir);
	}
	
	//垃圾箱文件生成
	@RequestMapping(value="/gabgefile",method=RequestMethod.POST)
	public @ResponseBody List<FileManage> gabagefile(HttpSession session) {	
		User u=(User)session.getAttribute("userinfo");
		Directory dir=dirdao.findByUserAndPidIsNull(u);
		List<FileManage> list=filedao.findByUserAndIsDeleteAndDir(u,1,dir);
		return list;
	}
	
	// 递归复制
	public void cursion(List<Directory> sondir, Map<String, String> map) throws IOException {
		for (Directory directory : sondir) {

			Map<String, String> sonmap = fileservice.copyfolder(directory, map.get("path"), map.get("id"));

			List<FileManage> grandsonfile = filedao.findByDir(directory);

			List<Directory> grandsondir = dirdao.findByPid(directory);
			for (FileManage f : grandsonfile) {
				fileservice.copy(f.getId(), map.get("id"), map.get("path"));
			}
			if (grandsondir.size() > 0) {
				this.cursion(grandsondir, sonmap);
			}
		}
	}

	public void delete(List<Directory> fatherdir,Integer type) {
		for (Directory directory : fatherdir) {
			List<FileManage> sonfile = filedao.findByDir(directory);
			directory.setIsDelete(type);
			dirdao.save(directory);
			for (FileManage fileManage : sonfile) {
				fileManage.setIsDelete(type);
				filedao.save(fileManage);
			}
			List<Directory> sondir = dirdao.findByPid(directory);
			if (sondir.size() > 0) {
				this.delete(sondir,type);
			}
		}
	}
	
	public void deletefunction(List<Directory> fatherdir) {
		for (Directory directory : fatherdir) {
			List<FileManage> sonfile = filedao.findByDir(directory);
			for (FileManage fileManage : sonfile) {
				filedao.delete(fileManage);
			}
			List<Directory> sondir = dirdao.findByPid(directory);
			if (sondir.size() > 0) {
				this.deletefunction(sondir);
			}
			dirdao.delete(directory);
		}
	}

	// 递归移动
	public void move(List<Directory> fatherdir) {
		for (Directory directory : fatherdir) {
			List<FileManage> grandsonfile = filedao.findByDir(directory);
			for (FileManage fileManage : grandsonfile) {
				filedao.delete(fileManage);
			}
			List<Directory> grandsondir = dirdao.findByPid(directory);

			if (grandsondir.size() > 0) {
				this.move(grandsondir);
			}
			dirdao.delete(directory);
		}
	}

}
